package com.shwread.android.ui.widget;

import android.app.Activity;
import android.app.Dialog;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.widget.NumberPicker.Formatter;
import com.shwread.android.ui.widget.NumberPicker.OnValueChangeListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import logic.bean.OnBusinessTimeInfo;
import logic.util.DateUtil;
import logic.util.Utils;


public class BookTimeDialog
        extends
        Dialog
{
	private NumberPicker             datePicker;
	private NumberPicker             timePicker, timePicker2;
	private View                     vOk;
	private View                     vCancel;
	
	private Activity                 activity;
	private Calendar                 calendar;
	private String[]                 mDateDisplayValues;
	private String[]                 mTimeDisplayValues;
	private String[]                 mTimeDisplayValues2;
	
	private List<OnBusinessTimeInfo> times;
	private List<OnBusinessTimeInfo> times2      = new ArrayList<OnBusinessTimeInfo>();
	private boolean                  isFirstDate = true;
	private int                      fromType    = 0;
	
	public BookTimeDialog(Activity activity, int day, List<OnBusinessTimeInfo> times) {
		super(activity, R.style.no_title_dialog);
		setCanceledOnTouchOutside(true);
		this.activity = activity;
		this.times = times;
		mDateDisplayValues = new String[day];
		findViews();
		setListeners();
		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
		
		calendar = Calendar.getInstance();
		updateDateControl(day, isTodayPassed(times));
		datePicker.setFocusable(true);
		datePicker.setFocusableInTouchMode(true);
		datePicker.setFormatter(new Formatter()
		{
			
			@Override
			public String format(int arg0) {
				
				return null;
			}
		});
		if (isTodayPassed(times))
		{
			timePicker.setMinValue(0);
			timePicker.setMaxValue(times.size() - 1);
			timePicker.setVisibility(View.VISIBLE);
			timePicker2.setVisibility(View.GONE);
			updateTimeControl(times.size(), times);
		}
		else
		{
			resetTimePicker();
		}
		timePicker.setFocusable(true);
		timePicker.setFocusableInTouchMode(true);
		timePicker.setFormatter(new Formatter()
		{
			
			@Override
			public String format(int arg0) {
				return null;
			}
		});
	}
	
	private void updateDateControl(int day, boolean isPassed) {
		datePicker.setMinValue(0);
		datePicker.setMaxValue(day - 1);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(calendar.getTimeInMillis());
		datePicker.setDisplayedValues(null);
		if (isPassed)
		{
			for (int i = 0; i < day; i++)
			{
				cal.add(Calendar.DAY_OF_YEAR, 1);
				mDateDisplayValues[i] = (String) DateFormat.format("yyyy-MM-dd", cal);
			}
		}
		else
		{
			for (int i = 0; i < day; i++)
			{
				mDateDisplayValues[i] = (String) DateFormat.format("yyyy-MM-dd", cal);
				cal.add(Calendar.DAY_OF_YEAR, 1);
			}
		}
		datePicker.setDisplayedValues(mDateDisplayValues);
		datePicker.setValue(0);
		datePicker.invalidate();
	}
	
	private void updateTimeControl(int day, List<OnBusinessTimeInfo> listTimes) {
		mTimeDisplayValues = new String[day];
		timePicker.setDisplayedValues(null);
		for (int i = 0; i < day; i++)
		{
			mTimeDisplayValues[i] = listTimes.get(i).getStartTime() + "-" + listTimes.get(i).getEndTime();
		}
		timePicker.setDisplayedValues(mTimeDisplayValues);
		timePicker.setValue(0);
		timePicker.invalidate();
		
	}
	
	private void updateTimeControl2(int day, List<OnBusinessTimeInfo> listTimes) {
		mTimeDisplayValues2 = new String[day];
		timePicker2.setDisplayedValues(null);
		for (int i = 0; i < day; i++)
		{
			mTimeDisplayValues2[i] = listTimes.get(i).getStartTime() + "-" + listTimes.get(i).getEndTime();
		}
		timePicker2.setDisplayedValues(mTimeDisplayValues2);
		timePicker2.setValue(0);
		timePicker2.invalidate();
		
	}
	
	private void findViews() {
		View view = LayoutInflater.from(activity).inflate(R.layout.online_book_number_dialog, null);
		datePicker = (NumberPicker) view.findViewById(R.id.date_picker);
		timePicker = (NumberPicker) view.findViewById(R.id.time_picker);
		timePicker2 = (NumberPicker) view.findViewById(R.id.time_picker2);
		vOk = view.findViewById(R.id.booking_time_ok);
		vCancel = view.findViewById(R.id.booking_time_cancel);
		setContentView(view);
	}
	
	private void setListeners() {
		ClickListener l = new ClickListener();
		
		vOk.setOnClickListener(l);
		vCancel.setOnClickListener(l);
		datePicker.setOnValueChangedListener(new OnValueChangeListener()
		{
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (newVal == 0 && !isTodayPassed(times))
				{
					isFirstDate = true;
					resetTimePicker();
				}
				else
				{
					isFirstDate = false;
					timePicker.setMinValue(0);
					timePicker.setMaxValue(times.size() - 1);
					timePicker.setVisibility(View.VISIBLE);
					timePicker2.setVisibility(View.GONE);
					updateTimeControl(times.size(), times);
				}
				
			}
		});
	}
	
	void clickOk() {
		dismiss();
		if (callBack != null)
		{
			System.out.println("datePicker.getValue()===========" + datePicker.getValue());
			if (isFirstDate && !isTodayPassed(times))
			{
				fromType = 1;
				callBack.callBack(mDateDisplayValues[datePicker.getValue()] + " " + times2.get(timePicker2.getValue()).getStartTime() + "-" + times2.get(timePicker2.getValue()).getEndTime());
			}
			else
			{
				fromType = 0;
				callBack.callBack(mDateDisplayValues[datePicker.getValue()] + " " + times.get(timePicker.getValue()).getStartTime() + "-" + times.get(timePicker.getValue()).getEndTime());
			}
		}
	}
	
	void clickCancel() {
		dismiss();
	}
	
	private class ClickListener
	        implements
	        View.OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.booking_time_ok:
					clickOk();
					break;
				case R.id.booking_time_cancel:
					clickCancel();
					break;
			}
		}
	}
	
	private CallBack callBack;
	
	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}
	
	public void show(String time) {
		if (Utils.isNotEmpty(time))
		{
			String[] split = time.split(" ");
			String currDate = split[0];
			String currTime = split[1];
			for (int i = 0; i < mDateDisplayValues.length; i++)
			{
				if (mDateDisplayValues[i].equals(currDate))
				{
					datePicker.setValue(i);
					break;
				}
			}
			if (fromType == 0)
			{
				timePicker.setVisibility(View.VISIBLE);
				timePicker2.setVisibility(View.GONE);
				for (int i = 0; i < mTimeDisplayValues.length; i++)
				{
					if (mTimeDisplayValues[i].equals(currTime))
					{
						timePicker.setValue(i);
					}
				}
			}
			else
			{
				timePicker.setVisibility(View.GONE);
				timePicker2.setVisibility(View.VISIBLE);
				for (int i = 0; i < mTimeDisplayValues2.length; i++)
				{
					if (mTimeDisplayValues2[i].equals(currTime))
					{
						timePicker2.setValue(i);
					}
				}
			}
		}
		super.show();
	}
	
	private boolean isPassedTime(String time) {
		String[] split = time.split(":");
		int hour = Integer.parseInt(split[0]);
		int minute = Integer.parseInt(split[1]);
		int currHour = DateUtil.getHours();
		int currMinute = DateUtil.getMinutes();
		if (hour < currHour)
		{
			return true;
		}
		else if (hour == currHour)
		{
			if (minute < currMinute + 15)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	private boolean isTodayPassed(List<OnBusinessTimeInfo> listTimes) {
		boolean isPassed = true;
		for (int i = 0; i < listTimes.size(); i++)
		{
			if (!isPassedTime(listTimes.get(i).getStartTime()))
			{
				isPassed = false;
				break;
			}
		}
		return isPassed;
	}
	
	private void resetTimePicker() {
		times2.clear();
		for (int i = 0; i < times.size(); i++)
		{
			if (!isPassedTime(times.get(i).getStartTime()))
			{
				times2.add(times.get(i));
			}
		}
		timePicker2.setMinValue(0);
		timePicker2.setMaxValue(times2.size() - 1);
		timePicker.setVisibility(View.GONE);
		timePicker2.setVisibility(View.VISIBLE);
		updateTimeControl2(times2.size(), times2);
	}
	
	public static interface CallBack
	{
		void callBack(String date);
	}
}
